package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }
    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail, String tags) {

        //目的地id
        Strategy strategy = strategyService.get(detail.getStrategy().getId());
        Destination dest = strategy.getDest();
        detail.setDest(dest);


        //摘要:
        //是由运营的人员页面编写,
        //程序员解决,从内容截取200字符串
        String conent = detail.getStrategyContent().getContent();

        if(conent.length() >200){
            detail.setSummary(conent.substring(0,200));
        }else{
            detail.setSummary(conent);
        }

        //接着就是判断是否是国内 的操作
        List<Destination> list = destinationService.getToasts(dest.getId());
        Destination destination = list.get(0);
        detail.setIsabroad(destination.getId() !=1L);


        //创建时间是创建的时候才会有,修改的时候 是不应该改的,所以我们就要在这里面去写的
        if(detail.getId() == null){
            //创建时间
            detail.setCreateTime(new Date());

            strategyDetailMapper.insert(detail);
        }else {

            strategyDetailMapper.updateByPrimaryKey(detail);
            //内容处理
            StrategyContent content = detail.getStrategyContent();
            content.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(content);

            //出现了编辑的问题,就是修改的时候会出现重复的,,所以我们修改前删除就行了
            //打破关系
            strategyDetailMapper.deleteRelation(detail.getId());
        }
        //接着就是去处理我们要写的标签的内容了
        if(StringUtils.hasLength(tags)){
            String[] split = tags.split(",");
            for (String s : split){
                StrategyTag tag = new StrategyTag();
                tag.setName(s);
                strategyTagService.saveOrUpdate(tag);
                //维护关系
                strategyDetailMapper.insertRelation(detail.getId(),tag.getId());
            }
        }
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> queryViewNumTop3(Long destId) {
        return strategyDetailMapper.selectByViewNumTop3(destId);
    }

    @Override
    public void updateStrategyStatisVO(StrategyStatisPersistenceVO pvo) {
        strategyDetailMapper.updateStrategyStatisVO(pvo);
    }
}
